package com.app.acitivty;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TableRow;
import android.widget.TextView;

import com.app.activity.R;
import com.app.components.TopMenu;
import com.app.utils.ActivityTools;

/**
 * 预定机票
 * 
 * @author Administrator
 * 
 */
public class TicketScheduledActivity extends Activity implements
		OnClickListener {

	private final int START_DATE_DIALOG = 1;
	private final int END_DATE_DIALOG = 0;
	private Button btnSearch;

	private TextView tvStartDate;
	private TextView tvEndDate;
	private TextView tvTicketType;

	private TableRow tbStartDate;
	private TableRow tbEndDate;
	private TableRow tbTicketType;

	private void initView() {
		btnSearch = (Button) findViewById(R.id.btnSearch);
		tvStartDate = (TextView) findViewById(R.id.tvStartDate);
		tvEndDate = (TextView) findViewById(R.id.tvEndDate);
		tvTicketType = (TextView) findViewById(R.id.tvTickeType);
		tbStartDate = (TableRow) findViewById(R.id.tbStartDate);
		tbEndDate = (TableRow) findViewById(R.id.tbEndDate);
		tbTicketType=(TableRow)findViewById(R.id.tbTicketType);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ticket_scheduled);
		initView();
		tbStartDate.setOnClickListener(this);
		tbEndDate.setOnClickListener(this);
		btnSearch.setOnClickListener(this);

		TopMenu topmenu = new TopMenu(this, "返回", new OnClickListener() {

			@Override
			public void onClick(View v) {
				TicketScheduledActivity.this.finish();
			}
		}, R.drawable.top_button_unselect, R.drawable.top_button_selected,
				"预定机票");
		topmenu.onCreate();

		final DialogInterface.OnClickListener onselect = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					tvTicketType.setText("单程");
					break;
				case 1:
					tvTicketType.setText("返程");
					break;
				
				}
			}

		};
		tbTicketType.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String[] choices = { "单程", "返程"};
				AlertDialog dialog = new AlertDialog.Builder(
						TicketScheduledActivity.this)
						.setTitle("机票类型")
						.setItems(choices, onselect).create();
				dialog.show();
			}
		});


	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case START_DATE_DIALOG:
			Calendar c = Calendar.getInstance();
			dialog = new DatePickerDialog(this,
					new DatePickerDialog.OnDateSetListener() {
						public void onDateSet(DatePicker dp, int year,
								int month, int dayOfMonth) {
							tvStartDate.setText(year + "-" + (month + 1) + "-"
									+ dayOfMonth);
						}
					}, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
					c.get(Calendar.DAY_OF_MONTH));
			break;
		case END_DATE_DIALOG:
			Calendar c1 = Calendar.getInstance();
			dialog = new DatePickerDialog(this,
					new DatePickerDialog.OnDateSetListener() {
						public void onDateSet(DatePicker dp, int year,
								int month, int dayOfMonth) {
							tvEndDate.setText(year + "-" + (month + 1) + "-"
									+ dayOfMonth);
						}
					}, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH),
					c1.get(Calendar.DAY_OF_MONTH));
			break;
		}
		return dialog;

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSearch:
			ActivityTools.gotoActivity(this, TicketListActivity.class);
			break;
		case R.id.tbStartDate:
			showDialog(START_DATE_DIALOG);
			break;
		case R.id.tbEndDate:
			showDialog(END_DATE_DIALOG);
			break;
		default:
			break;
		}

	}

}